//
//  UILabel+lvfj.m
//  GDKStock
//
//  Created by lvfeijun on 2017/7/3.
//  Copyright © 2017年 lvfj. All rights reserved.
//

#import "UILabel+lvfj.h"
#import "StringUtils.h"

@implementation UILabel (lvfj)

-(void)setText:(NSString *)text textColor:(UIColor *)textColor fontSize:(CGFloat)fontSize{
    self.text = text;
    [self setTextColor:textColor font:[UIFont systemFontOfSize:fontSize]];
}
-(void)setText:(NSString *)text textColor:(UIColor *)textColor font:(UIFont *)font{
    self.text = text;
    [self setTextColor:textColor font:font];
}

-(void)setTextColor:(UIColor *)textColor font:(UIFont *)font{
    self.textColor = textColor;
    self.font = font;
}

-(void)setNoText:(NSString *)text{
    if (![StringUtils isEmptyString:text]) {
        self.text = text;
    }
}

#pragma mark 设置 省略号
/*
 label.lineBreakMode =NSLineBreakByCharWrapping;//其中lineBreakMode可选值为
 linBreakMode enum{
     //保留整个单词，以空格为边界
     NSLineBreakByWordWrapping = 0,
     //保留整个字符
      NSLineBreakByCharWrapping,
     //以边界为止
     NSLineBreakByClipping,
     //省略开头，以省略号代替
     NSLineBreakByTruncatingHead,
     //省略结尾，以省略号代替
     NSLineBreakByTruncatingTail,
     //省略中间，以省略号代替
     NSLineBreakByTruncatingMiddle
    }

 */
-(void)setEllipsisAfterSetAttrbute:(NSMutableAttributedString *)attributedText{
    self.attributedText = attributedText;
    //因为label设置了属性，导致lineBreakMode失效了，不能正常获取到行的高度。所以需要重新设置一下才能正常显示省略号。
    self.lineBreakMode = NSLineBreakByTruncatingTail;
    
    //按字母，自动换行
    self.replyLb.lineBreakMode = NSLineBreakByCharWrapping;
}

-(void)setEllipsisOnText{
    self.lineBreakMode = NSLineBreakByTruncatingTail;
}

#pragma setParagraph 设置行高，行间距

-(void)setParagraph:(CGFloat)paragraph text:(NSString *)text{
    [self setParagraph:paragraph text:text textColor:nil font:nil];
}

-(void)setParagraph:(CGFloat)paragraph text:(NSString *)text textColor:(UIColor *)textColor font:(UIFont *)font{
    self.numberOfLines = 0;
    if (!textColor) {
        textColor = self.textColor;
    }
    if (!font) {
        font = self.font;
    }
    NSMutableAttributedString *attributeString;
    if ([text isKindOfClass:[NSAttributedString class]]) {
         attributeString = [[NSMutableAttributedString alloc] initWithAttributedString:(NSAttributedString *)text];
    }
    else{
        attributeString = [[NSMutableAttributedString alloc] initWithString:text];
    }
    //设置行高
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:paragraph];//行间距
    NSRange range = NSMakeRange(0, attributeString.length);
    [attributeString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    [attributeString addAttribute:NSForegroundColorAttributeName value:textColor range:range];
    [attributeString addAttribute:NSFontAttributeName value:font range:range];
    [self setAttributedText:attributeString];
    [self sizeToFit];
}
-(NSMutableAttributedString *)attributedStringWithString:(NSString *)str Font:(UIFont *)font LineSpacing:(NSInteger)lineSpacing MaxWidth:(CGFloat)w{
    if (!str) {
        str=@"";
    }
    NSDictionary *dict = @{NSFontAttributeName: font};
    CGSize size = [str sizeWithAttributes:dict];
   
    NSMutableAttributedString * attributeTitleStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attributeTitleStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributeTitleStr.length)];
   
     // 如果大于一行，设置行间距
    if(size.width>w){
        NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle1 setLineSpacing:lineSpacing];
        [attributeTitleStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attributeTitleStr length])];
    }
    return attributeTitleStr;
}
+(CGSize)getHeightWithParagraph:(CGFloat)paragraph string:(NSString *)string
                          width:(CGFloat)width font:(UIFont *)font{
    UILabel *lb = [[UILabel alloc]init];//计算高度度
    [lb setParagraph:paragraph text:string textColor:nil font:font];
    return [lb sizeThatFits:CGSizeMake(width, MAXFLOAT)];
}

#pragma mark get height size 根据宽度计算高度

-(CGSize)getHeightWithString:(NSString *)string
                        width:(CGFloat)width
                         font:(UIFont *)font{
    self.text = [StringUtils emptyStringReplaceNSNull:[StringUtils getString:string]];
    self.font = font;
    self.numberOfLines = 0;
    
    return [self sizeThatFits:CGSizeMake(width, MAXFLOAT)];
}

-(CGSize)getSizeWithString:(NSString *)string
                      font:(UIFont *)font{
    return [self getHeightWithString:string width:MAXFLOAT font:font];
}

+(CGSize)getHeightWithString:(NSString *)string
                       width:(CGFloat)width
                        font:(UIFont *)font{
    UILabel *lb = [[UILabel alloc]init];
    return [lb getHeightWithString:(NSString *)string
                             width:(CGFloat)width
                              font:(UIFont *)font];
}

+(CGSize)getSizeWithString:(NSString *)string
                      font:(UIFont *)font{
    UILabel *lb = [[UILabel alloc]init];
    return [lb getSizeWithString:(NSString *)string
                            font:(UIFont *)font];
}

@end
